package com.alibaba.dubbo.common.serialize.support.kryo;

import com.alibaba.dubbo.common.serialize.Cleanable;
import com.alibaba.dubbo.common.serialize.ObjectInput;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.KryoDataInput;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * Created by sunlei on 2016/2/18.
 */
public class KryoObjectInput implements ObjectInput,Cleanable{
    private final Kryo kryo=KryoFactory.getInstance().create();
    private final Input input;
    public KryoObjectInput(InputStream is){
        input=new Input(is);
    }

    public Object readObject() throws IOException, ClassNotFoundException {
        return kryo.readClassAndObject(input);
    }

    public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
        return (T)readObject();
    }

    public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
        return (T)readObject(cls);
    }

    public boolean readBool() throws IOException {
        return input.readBoolean();
    }

    public byte readByte() throws IOException {
        return input.readByte();
    }

    public short readShort() throws IOException {
        return input.readShort();
    }

    public int readInt() throws IOException {
        return input.readInt();
    }

    public long readLong() throws IOException {
        return input.readLong();
    }

    public float readFloat() throws IOException {
        return input.readFloat();
    }

    public double readDouble() throws IOException {
        return input.readDouble();
    }

    public String readUTF() throws IOException {
        return input.readString();
    }

    public byte[] readBytes() throws IOException {
        return input.getBuffer();
    }

    public void cleanup() {
        KryoFactory.getInstance().release(kryo);
    }
}
